package com.juc.semaphore.semaphore1;

import java.util.concurrent.Semaphore;

public class Service {
    /**
     * semaphore 的同步性 保证线程排队的运行
     */
    private Semaphore semaphore = new Semaphore(1);

    public void testMethod(){
        try {
            System.out.println("***1 "+semaphore.toString());
            semaphore.acquire();
            System.out.println("semaphore = " + semaphore.hasQueuedThreads());
            System.out.println(semaphore.getQueueLength());
            System.out.println("***2 "+semaphore.toString());
            System.out.println(Thread.currentThread().getName() + " begin timer " + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println(Thread.currentThread().getName() + " end timer " + System.currentTimeMillis());
            semaphore.release();
            System.out.println("***3 "+semaphore.toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
